package com.example.cofeed.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ObjectUtils;
import  com.example.cofeed.util.ToastUtils;;
import com.example.cofeed.R;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.lzy.okgo.model.Response;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InputPhoneActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText mEtPhone;
    //0-密码找回，1-密码修改
    private int mType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register_step_phone;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        if (getIntent() != null) {
            mType = getIntent().getIntExtra("type", 0);
        }

        if (mType == 0) {
            setMainTitle("密码找回");
        } else {
            setMainTitle("修改密码");
        }

    }

    @OnClick(R.id.tv_next)
    public void onViewClicked() {
        String phone = mEtPhone.getText().toString().trim();
        if (ObjectUtils.isEmpty(phone)){
            ToastUtils.showShort("请输入手机号码");
        }

        if (mType == 0){
            ApiService.password_mobile_status(this, phone, new DialogCallback<LzyResponse<Object>>() {
                @Override
                public void onSuccess(Response<LzyResponse<Object>> response) {
                    Map<String, String> map = (Map<String, String>)response.body().data;
                    String i = map.get("accountid");
                    int accountid = Integer.valueOf(i);

                    if (accountid > 0){
                        Intent intent = new Intent(mContext, ForgetPwdActivity.class);
                        intent.putExtra(IntentKeyConstant.PHONE, phone);
                        intent.putExtra(IntentKeyConstant.ACCOUNT_ID, accountid);
                        startActivity(intent);
                        finish();

                    }else {
                        ToastUtils.showShort("您的手机没有绑定账号");
                    }
                }
            });
        }else {
            ApiService.password_mobile_status(this, phone, new DialogCallback<LzyResponse<Object>>() {
                @Override
                public void onSuccess(Response<LzyResponse<Object>> response) {
                    Map<String, String> map = (Map<String, String>)response.body().data;
                    String i = map.get("accountid");
                    int accountid = Integer.valueOf(i);

                    if (accountid > 0){
                        Intent intent = new Intent(mContext, ForgetPwdActivity.class);
                        intent.putExtra(IntentKeyConstant.PHONE, phone);
                        intent.putExtra(IntentKeyConstant.TYPE, mType);
                        intent.putExtra(IntentKeyConstant.ACCOUNT_ID, accountid);
                        startActivity(intent);
                        finish();

                    }else {
                        ToastUtils.showShort("您的手机没有绑定账号");
                    }
                }
            });
        }


    }
}
